package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.util.Set;
@Component
public class ClearImgJob {
    //注入jedisPool用于获取resis里的数据
    @Autowired
    private JedisPool jedisPool;

    private void clearImg() {
        //调用jedis的方法,找到缓存不同的数据,即是要删除得数据
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        //对set集合进行判空,如果不为空,说明里面有值
        if (set != null && set.size() > 0){
            //遍历
            for (String s : set) {
                //删除七牛云的数据
                QiniuUtils.deleteFileFromQiniu(s);
                //删除redis缓存的数据
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,s);
            }

        }
    }
}
